package com.sphd.education;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import org.litepal.LitePalApplication;
import org.litepal.tablemanager.Connector;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 明德教育程序入口
 * Created by Administrator on 2015/10/9.
 */
public class EducationApplication extends LitePalApplication {
    private Context context;
    public static RequestQueue requestQueue;
    @Override
    public void onCreate() {
        super.onCreate();
        context=getApplicationContext();

        requestQueue = Volley.newRequestQueue(context);
        SQLiteDatabase db = Connector.getDatabase();
        inporDatabase(context);
        File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageurl/Education");
        ImageLoaderConfiguration config = new ImageLoaderConfiguration

                .Builder(context)
                .memoryCacheExtraOptions(200, 200) // max width, max height，即保存的每个缓存文件的最大长宽
                .discCacheExtraOptions(200, 200, Bitmap.CompressFormat.PNG, 75, null) // Can slow ImageLoader, use it carefully (Better don't use it)/设置缓存的详细信息，最好不要设置这个
                .threadPoolSize(3)//线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)
                .discCacheSize(50 * 1024 * 1024)
                .discCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .discCacheFileCount(100) //缓存的文件数量
                .discCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
                .writeDebugLogs() // Remove for release app
                .build();//开始构建
        // Initialize ImageLoader with configuration.
        // 以下是您原先的代码实现，保持不变 注意严禁将PushManager.startWork 置于此处，否则无法正常绑定。
        ImageLoader.getInstance().init(config);
    }

    public void inporDatabase(Context context) {
        //存放数据库的目录
        String dirPath = "/data/data/" + context.getPackageName() + "/databases";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        //数据库文件
        File file = new File(dir, "china_province_city_zone.db");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            //加载需要导入的数据库
            InputStream is = context.getApplicationContext().getResources().openRawResource(R.raw.china_province_city_zone);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffere = new byte[is.available()];
            is.read(buffere);
            fos.write(buffere);
            is.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static RequestQueue getRequestQueue() {
        return requestQueue;
    }
}
